package com.yida.gwj.basis.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yida.common.core.text.Convert;
import com.yida.common.core.utils.DateUtils;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.basis.domain.GwjInitBusiness;
import com.yida.gwj.basis.domain.GwjInitBusinessLog;
import com.yida.gwj.basis.mapper.GwjInitBusinessLogMapper;
import com.yida.gwj.basis.service.IGwjInitBusinessLogService;
import com.yida.gwj.common.constant.GwjRemarkConstants;
import com.yida.gwj.common.domain.dto.GwjUserDTO;
import com.yida.gwj.common.enums.GwjBusinessLogResult;

/**
 * 签署意见Service业务层处理
 *
 * @author lqq
 * @date 2022-11-15
 */
@Service
public class GwjInitBusinessLogServiceImpl implements IGwjInitBusinessLogService {

    @Resource
    private GwjInitBusinessLogMapper businessLogMapper;

    /**
     * 查询业务日志
     *
     * @param bizId 业务ID
     * @return /
     */
    @Override
    public List<GwjInitBusinessLog> getLogList(Long bizId) {
        GwjInitBusinessLog businessLog = new GwjInitBusinessLog();
        businessLog.setBizId(bizId);
        return businessLogMapper.selectList(businessLog);
    }

    /**
     * 根据步骤查询的终提交人
     *
     * @param bizId 业务ID
     * @param stepId 步骤ID
     * @return 操作者ID
     */
    @Override
    public GwjUserDTO getPreviousSubmitUser(Long bizId, Long stepId) {
        return businessLogMapper.selectUser(bizId, stepId, GwjBusinessLogResult.SUBMIT.getCode());
    }

    /**
     * 根据步业务ID删除日志
     *
     * @param bizId 业务ID
     * @return 操作者ID
     */
    @Override
    public int deleteByBizId(Long bizId) {
        return businessLogMapper.deleteByBizId(bizId);
    }

    /**
     * 送件日志
     *
     * @param businessLog 业务日志
     * @param business 业务信息
     */
    @Override
    public void addSendLog(GwjInitBusinessLog businessLog, GwjInitBusiness business) {
        Long currentUserId = SecurityUtils.getUserId();
        businessLog.setStepId(business.getStepId());
        businessLog.setOperId(currentUserId);
        businessLog.setOperName(business.getCurrentOperName());
        businessLog.setEndTime(new Date());
        // 获取上一次流程的最后时间
        businessLog.setAcceptTime(businessLogMapper.selectPreviousEndDateByBizId(business.getBizId()));
        businessLog.setContent("送件");
        businessLog.setResult(GwjBusinessLogResult.SEND.getCode());
        businessLog.setRemark(GwjRemarkConstants.SEND_BUSINESS
            .replace("{username}", SecurityUtils.getLoginUser().getSysUser().getRealName())
            .replace("{userId}", Convert.toStr(currentUserId)).replace("{username1}", businessLog.getRealName())
            .replace("{userId1}", Convert.toStr(businessLog.getUserId()))
            .replace("{businessId}", Convert.toStr(business.getBizId())));
        businessLog.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        businessLog.setCreateTime(DateUtils.getNowDate());
        businessLogMapper.insert(businessLog);
    }

    /**
     * 签署意见日志
     *
     * @param businessLog 日志信息
     */
    @Override
    public void addSignOpinionLog(GwjInitBusinessLog businessLog) {
        Long currentUserId = SecurityUtils.getUserId();
        String currentUser = SecurityUtils.getLoginUser().getSysUser().getRealName();
        businessLog.setOperId(currentUserId);
        businessLog.setOperName(currentUser);
        businessLog.setEndTime(new Date());
        // 获取上一次流程的最后时间
        businessLog.setAcceptTime(businessLogMapper.selectPreviousEndDateByBizId(businessLog.getBizId()));
        businessLog.setResult(GwjBusinessLogResult.SAVE.getCode());
        businessLog.setRemark(GwjRemarkConstants.SIGN_OPINION.replace("{username}", currentUser)
            .replace("{userId}", Convert.toStr(currentUserId)).replace("{content}", businessLog.getContent())
            .replace("{businessId}", Convert.toStr(businessLog.getBizId())));
        businessLog.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        businessLog.setCreateTime(DateUtils.getNowDate());
        businessLogMapper.insert(businessLog);
    }

    /**
     * 回退日志
     *
     * @param businessLog 业务信息
     */
    @Override
    public void addFallbackLog(GwjInitBusinessLog businessLog) {
        Long currentUserId = SecurityUtils.getUserId();
        String currentUser = SecurityUtils.getLoginUser().getSysUser().getRealName();
        businessLog.setOperId(currentUserId);
        businessLog.setOperName(currentUser);
        businessLog.setEndTime(new Date());
        // 获取上一次流程的最后时间
        businessLog.setAcceptTime(businessLogMapper.selectPreviousEndDateByBizId(businessLog.getBizId()));
        if (StringUtils.isBlank(businessLog.getContent())) {
            businessLog.setContent("回退");
        }
        businessLog.setResult(GwjBusinessLogResult.FALLBACK.getCode());
        businessLog.setRemark(GwjRemarkConstants.FALLBACK.replace("{username}", currentUser)
            .replace("{userId}", Convert.toStr(currentUserId)).replace("{username1}", businessLog.getRealName())
            .replace("{userId1}", Convert.toStr(businessLog.getUserId()))
            .replace("{businessId}", Convert.toStr(businessLog.getBizId())));
        businessLog.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        businessLog.setCreateTime(DateUtils.getNowDate());
        businessLogMapper.insert(businessLog);
    }

    /**
     * 保存日志
     *
     * @param business 业务信息
     */
    @Override
    public void addSaveLog(GwjInitBusiness business) {
        Long currentUserId = SecurityUtils.getUserId();
        String currentUser = SecurityUtils.getLoginUser().getSysUser().getRealName();
        GwjInitBusinessLog businessLog = new GwjInitBusinessLog();
        businessLog.setBizId(business.getBizId());
        Date previousEndDate = businessLogMapper.selectPreviousEndDateByBizId(businessLog.getBizId());
        businessLog.setAcceptTime(StringUtils.isNull(previousEndDate) ? business.getApplicantTime() : previousEndDate);
        businessLog.setEndTime(new Date());
        businessLog.setStepId(business.getStepId());
        businessLog.setOperId(currentUserId);
        businessLog.setOperName(currentUser);
        businessLog.setResult(GwjBusinessLogResult.SAVE.getCode());
        businessLog.setRemark(
            GwjRemarkConstants.SAVE.replace("{username}", currentUser).replace("{userId}", Convert.toStr(currentUserId))
                .replace("{businessId}", Convert.toStr(business.getBizId())));
        businessLog.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        businessLog.setCreateTime(DateUtils.getNowDate());
        businessLogMapper.insert(businessLog);
    }

    /**
     * 提交日志
     *
     * @param businessLog 业务日志
     * @param business 业务信息
     */
    @Override
    public void addSubmitLog(GwjInitBusiness business, GwjInitBusinessLog businessLog) {
        businessLog.setAcceptTime(businessLogMapper.selectPreviousEndDateByBizId(business.getBizId()));
        businessLog.setOperId(SecurityUtils.getUserId());
        businessLog.setOperName(SecurityUtils.getLoginUser().getSysUser().getRealName());
        businessLog.setEndTime(new Date());
        businessLog.setResult(GwjBusinessLogResult.SUBMIT.getCode());
        if (StringUtils.isAnyBlank(businessLog.getRealName(), Convert.toStr(businessLog.getUserId()))) {
            businessLog.setRemark(GwjRemarkConstants.SUBMIT_NOBODY
                .replace("{username}", SecurityUtils.getLoginUser().getSysUser().getRealName())
                .replace("{userId}", Convert.toStr(SecurityUtils.getUserId()))
                .replace("{businessId}", Convert.toStr(business.getBizId())));
        } else {
            businessLog.setRemark(
                GwjRemarkConstants.SUBMIT.replace("{username}", SecurityUtils.getLoginUser().getSysUser().getRealName())
                    .replace("{userId}", Convert.toStr(SecurityUtils.getUserId()))
                    .replace("{username1}", businessLog.getRealName())
                    .replace("{userId1}", Convert.toStr(businessLog.getUserId()))
                    .replace("{businessId}", Convert.toStr(businessLog.getBizId())));
        }
        businessLog.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        businessLog.setCreateTime(DateUtils.getNowDate());
        businessLogMapper.insert(businessLog);
    }

    /**
     * 完结日志
     *
     * @param business 业务信息
     */
    @Override
    public void addFinishLog(GwjInitBusiness business) {
        GwjInitBusinessLog businessLog = new GwjInitBusinessLog();
        businessLog.setBizId(business.getBizId());
        Date data = new Date();
        businessLog.setAcceptTime(data);
        businessLog.setEndTime(data);
        businessLog.setStepId(business.getStepId());
        businessLog.setOperId(business.getCurrentOperId());
        businessLog.setOperName(business.getCurrentOperName());
        businessLog.setResult(GwjBusinessLogResult.FINISH.getCode());
        businessLog.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        businessLog.setCreateTime(DateUtils.getNowDate());
        businessLogMapper.insert(businessLog);
    }
}
